/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-09-05
 * Time: 19:26
 */

class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
public class Test01 {
    public static boolean chkPalindrome(ListNode head) {
        if(head == null) {
            return false;
        }
        if(head.next == null) {
            return true;
        }
        //先找到中间位置
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        //将中间位置后面的节点逆置
        ListNode cul = slow.next;
        slow.next = null;
        while(cul != null) {
            ListNode pex = cul.next;
            cul.next = slow;
            slow = cul;
            cul = pex;
        }
        //从两边开始判断
        while(head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    public static void main(String[] args) {
        ListNode head1 = new ListNode(11);
        ListNode head2 = new ListNode(22);
        ListNode head3 = new ListNode(33);
        ListNode head4 = new ListNode(22);
        ListNode head5 = new ListNode(11);
        head1.next = head2;
        head2.next = head3;
        head3.next = head4;
        head4.next = head5;
        System.out.println(chkPalindrome(head1));

    }
}
